<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Bara extends CI_Controller {

	public function __construct() {
		parent::__construct();
		$this->load->library('form_validation');
	//if ($this->session->userdata('isLoggedIn')){
            //$menu['isLoggedIn'] = true;
        //}else {
        //	redirect('site');
        //}
	}

	public function index(){
	
		
		$this->load->model('barangku','',true);
		$data['karyawan'] = $this->barangku->get_all();
		
		$this->load->view('header');
		$menu['isLoggedIn'] = true;
		$this->load->view('menu',$menu);
		$this->load->view('barangku',$data);
		$this->load->view('footer');
	}

	public function insert(){
		$data = array(
				'id_karyawan' 			=> $this->input->post('id_karyawan'),
				'nama' 					=> $this->input->post('nama'),
				'identitas' 			=> $this->input->post('identitas'),
				'alamat'				=> $this->input->post('alamat'),
				'kecamatan'				=> $this->input->post('kecamatan'),
				'kota'					=> $this->input->post('kota'),
				'telp'					=> $this->input->post('telp'),
				'tanggal_masuk'			=> $this->input->post('tanggal_masuk'),
				'jabatan'				=> $this->input->post('jabatan'),
				'cabang'				=> $this->input->post('cabang'),
				'username'				=> $this->input->post('username'),
				'password'				=> $this->input->post('password'),
				'role'					=> $this->input->post('role')
			);
		$this->load->model('barangku','',true);
		$this->form_validation->set_rules('id_karyawan', 'id_karyawan', 'trim|required|is_unique[karyawan.id_karyawan]');
		if ($this->form_validation->run() == FALSE) {
			$menu['error'] = true;
			$this->load->model('barangku','',true);
			$data['karyawan'] = $this->barangku->get_all();
			$this->load->view('header');
			$this->load->view('menu',$menu);
			$this->load->view('barangku',$data);
			$this->load->view('footer');
		} else {
			$this->barangku->insert($data);
			redirect('bara');
		}	
	}

	public function delete($data){
		$this->load->model('barangku','',true);
		$this->barangku->delete($data);
		redirect('bara');
	}

	public function edit($id){
		$this->load->model('barangku','',true);
		$data['detail'] = $this->barangku->get_by_id($id);
		$data['karyawan'] = $this->barangku->get_all();
		$this->load->view('header');
		$menu['isLoggedIn'] = true;
		$this->load->view('menu',$menu);
		$this->load->view('barangku',$data);
		$this->load->view('footer');
	}

	public function update(){
		$data = array(
				'id_karyawan' 			=> $this->input->post('id_karyawan'),
				'nama' 					=> $this->input->post('nama'),
				'identitas' 			=> $this->input->post('identitas'),
				'alamat'				=> $this->input->post('alamat'),
				'kecamatan'				=> $this->input->post('kecamatan'),
				'kota'					=> $this->input->post('kota'),
				'telp'					=> $this->input->post('telp'),
				'tanggal_masuk'			=> $this->input->post('tanggal_masuk'),
				'jabatan'				=> $this->input->post('jabatan'),
				'cabang'				=> $this->input->post('cabang'),
				'username'				=> $this->input->post('username'),
				'password'				=> $this->input->post('password'),
				'role'					=> $this->input->post('role')
			);
		$id = $this->input->post('id_karyawan');
		$this->load->model('barangku','',true);
		$this->form_validation->set_rules('id_karyawan', 'id_karyawan', 'trim|required');
		if ($this->form_validation->run() == FALSE) {
			$menu['error'] = true;
			$this->load->model('barangku','',true);
			$data['karyawan'] = $this->barangku->get_all();
			$this->load->view('header');
			$this->load->view('menu',$menu);
			$this->load->view('barangku',$data);
			$this->load->view('footer');
		} else {
			$this->barangku->update($id,$data);
			redirect('bara');
		}
		
	}
}
